package com.atguigu.spring.aop.anno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author yanglin
 * @create 2023-12-12 15:34 星期二
 * description: 增强类
 */
@Component
@Aspect
@Order(3)
public class UserProxy {
    
    //相同切入点抽取
    @Pointcut(value = "execution(* com.atguigu.spring.aop.anno.AopUser.add(..))")
    public void pointDemo(){
        
    }
    

    /**
     * 前置通知  @Before()
     */
    //@Before(value = "execution(* com.atguigu.spring.aop.anno.AopUser.add(..))")    //切入点表达式
    @Before(value = "pointDemo()")
    public void before(){
        System.out.println("user before....");
    }
    
    //最终通知  （有异常也执行）
    //@After(value = "execution(* com.atguigu.spring.aop.anno.AopUser.add(..))")
    @After(value = "pointDemo()")
    public void after(){
        System.out.println("user after....."); 
    }
    
    //后置通知  （返回通知）  （有异常就不执行）
    //@AfterReturning(value = "execution(* com.atguigu.spring.aop.anno.AopUser.add(..))")
    @AfterReturning(value = "pointDemo()")
    public void afterReturning(){
        System.out.println("user afterReturning......");
    }
    
    //异常通知   （只有发生异常才会执行）
    //@AfterThrowing(value = "execution(* com.atguigu.spring.aop.anno.AopUser.add(..))")
    @AfterThrowing(value = "pointDemo()")
    public void afterThrowing() {
        System.out.println("user afterThrowing....");
    }
    
    //环绕通知
    //@Around(value = "execution(* com.atguigu.spring.aop.anno.AopUser.add(..))")
    @Around(value = "pointDemo()")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("user 环绕之前......");
        //被增强的方法执行
        proceedingJoinPoint.proceed();
        System.out.println("user 环绕之后......");
    }
    
}
